/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/gq/video-monitoring/api/gqVideoCamera.ts";

import { pageAll as getVideoTypeList } from "$/gq/video-monitoring/api/gqVideoType.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  const videoTypeList = ref<any[]>([]);
  onMounted(async () => {
    const res = await getVideoTypeList({});
    if (res.code === 200 && res.data && res.data.list) {
      videoTypeList.value = res.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { label: () => t("video-monitoring.camera.fields.name"), prop: "name" },
    {
      label: () => t("video-monitoring.camera.fields.type"),
      prop: "type",
      cellRender: ({ row }) => {
        const videoTypeName = videoTypeList.value.find(
          (videoType) => videoType.id === row.type
        );
        return videoTypeName?.name || `摄像头类型${row.type}`;
      },
    },
    {
      label: () => t("video-monitoring.camera.fields.short_name"),
      prop: "short_name",
    },

    {
      label: () => t("video-monitoring.camera.fields.device_code"),
      prop: "device_code",
    },
    {
      label: "播放地址",
      prop: "play_url",
    },

    {
      label: () => t("video-monitoring.camera.fields.device_serial"),
      prop: "device_serial",
    },
    {
      label: () => t("video-monitoring.camera.fields.manufacturer"),
      prop: "manufacturer",
    },
    {
      label: () => t("video-monitoring.camera.fields.ip_address"),
      prop: "ip_address",
    },
    { label: () => t("video-monitoring.camera.fields.port"), prop: "port" },
    {
      label: () => t("video-monitoring.camera.fields.username"),
      prop: "username",
    },
    {
      label: () => t("video-monitoring.camera.fields.location"),
      prop: "location",
    },
    {
      label: () => t("video-monitoring.camera.fields.longitude"),
      prop: "longitude",
      render: ({ row }) => (row.longitude ? `${row.longitude}°` : "-"),
    },
    {
      label: () => t("video-monitoring.camera.fields.latitude"),
      prop: "latitude",
      render: ({ row }) => (row.latitude ? `${row.latitude}°` : "-"),
    },
    {
      label: () => t("video-monitoring.camera.fields.status"),
      prop: "status",
      cellRender: ({ row }) => {
        const orderSource = {
          0: { text: "离线", type: "danger" },
          1: { text: "在线", type: "success" },
        };
        return h(
          ElTag,
          { type: orderSource[row.status].type },
          () => orderSource[row.status].text
        );
      },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("video-monitoring:camera:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("video-monitoring:camera:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
